package view;

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import java.awt.Point;
import java.awt.Font;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.PanelUI;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

import model.CriminalRegModel;
import model.criminalModel;
import model.userModel;

import com.toedter.calendar.JDateChooser;
import common.loadData;

import dao.Criminal_RegDao;
import dao.encrypt;
import dao.userdao;

public class addCriminal extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel icpon = null;
	private JPanel panelTitle = null;
	private JLabel lbtitle = null;
	private JLabel lbFirstname = null;
	private JLabel lbLastname = null;
	private JLabel lbsex = null;
	private JLabel lbBirth = null;
	private JLabel lbAddress = null;
	private JTextField txtFname = null;
	private JTextField txtLname = null;
	private JRadioButton RadioButtonMale = null;
	private JRadioButton RadioButtonFemale = null;
	private JTextField txtAddress = null;
	private JPanel PanelImage = null;
	private JLabel lbimage = null;
	private JTextField ttxImage = null;
	private JButton btnImg = null;
	private JButton btnOK = null;
	private JButton btnCancel = null;
	private JLabel LabelImage = null;
	private JLabel jLabelImage = null;
	private JDateChooser txtDate;
	private JLabel lbuser = null;
	private JTextField txtuser = null;

	public addCriminal() {
		super();
		initialize();
		loadDataByID();
	}

	private void initialize() {
		this.setSize(480, 417);
		this.setResizable(false);
		this.setName("adCriminal");
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/image/management.png")));
		this.setContentPane(getJContentPane());
		this.setTitle("Add Criminal");
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lbuser = new JLabel();
			lbuser.setBounds(new Rectangle(29, 301, 79, 25));
			lbuser.setText("User Enter:");
			jLabelImage = new JLabel();
			jLabelImage.setText("JLabel");
			LabelImage = new JLabel();
			LabelImage.setText("Image:");
			LabelImage.setSize(new Dimension(77, 24));
			LabelImage.setLocation(new Point(30, 260));
			lbAddress = new JLabel();
			lbAddress.setText("Address:");
			lbAddress.setSize(new Dimension(76, 23));
			lbAddress.setLocation(new Point(31, 224));
			lbBirth = new JLabel();
			lbBirth.setText("Birth of Date:");
			lbBirth.setSize(new Dimension(76, 23));
			lbBirth.setLocation(new Point(31, 189));
			lbsex = new JLabel();
			lbsex.setText("Sex");
			lbsex.setSize(new Dimension(76, 23));
			lbsex.setLocation(new Point(31, 151));
			lbLastname = new JLabel();
			lbLastname.setText("Last Name:");
			lbLastname.setSize(new Dimension(76, 23));
			lbLastname.setLocation(new Point(31, 112));
			lbFirstname = new JLabel();
			lbFirstname.setBounds(new Rectangle(31, 76, 76, 23));
			lbFirstname.setText("First Name:");
			icpon = new JLabel();
			icpon.setBounds(new Rectangle(92, 8, 43, 46));
			icpon.setIcon(new ImageIcon(getClass().getResource("/image/criminal.png")));
			icpon.setText("");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(icpon, null);
			jContentPane.add(getPanelTitle(), null);
			jContentPane.add(lbFirstname, null);
			jContentPane.add(lbLastname, null);
			jContentPane.add(lbsex, null);
			jContentPane.add(lbBirth, null);
			jContentPane.add(lbAddress, null);
			jContentPane.add(getTxtFname(), null);
			jContentPane.add(getTxtLname(), null);
			jContentPane.add(getRadioButtonMale(), null);
			jContentPane.add(getRadioButtonFemale(), null);
			jContentPane.add(getTxtDate(), null);
			jContentPane.add(getTxtAddress(), null);
			jContentPane.add(getPanelImage(), null);
			jContentPane.add(getBtnOK(), null);
			jContentPane.add(getBtnCancel(), null);
			jContentPane.add(getTtxImage(), null);
			jContentPane.add(getBtnImg(), null);
			jContentPane.add(LabelImage, null);
			jContentPane.add(jLabelImage, null);
			jContentPane.add(lbuser, null);
			jContentPane.add(getTxtuser(), null);
		}
		return jContentPane;
	}

	private JPanel getPanelTitle() {
		if (panelTitle == null) {
			lbtitle = new JLabel();
			lbtitle.setLocation(new Point(0, 0));
			lbtitle.setFont(new Font("Dialog", Font.BOLD, 18));
			lbtitle.setForeground(new Color(46, 130, 124));
			lbtitle.setText("Add Criminal");
			lbtitle.setSize(new Dimension(116, 27));
			panelTitle = new JPanel();
			panelTitle.setLayout(null);
			panelTitle.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.red), "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			panelTitle.setSize(new Dimension(114, 25));
			panelTitle.setLocation(new Point(162, 20));
			panelTitle.add(lbtitle, null);
		}
		return panelTitle;
	}

	private JTextField getTxtFname() {
		if (txtFname == null) {
			txtFname = new JTextField();
			txtFname.setSize(new Dimension(166, 24));
			txtFname.setLocation(new Point(134, 74));
		}
		return txtFname;
	}

	private JTextField getTxtLname() {
		if (txtLname == null) {
			txtLname = new JTextField();
			txtLname.setLocation(new Point(134, 113));
			txtLname.setSize(new Dimension(166, 24));
		}
		return txtLname;
	}

	private JRadioButton getRadioButtonMale() {
		if (RadioButtonMale == null) {
			RadioButtonMale = new JRadioButton();
			RadioButtonMale.setBounds(new Rectangle(135, 151, 78, 22));
			RadioButtonMale.setText("Male");
		}
		return RadioButtonMale;
	}

	private JRadioButton getRadioButtonFemale() {
		if (RadioButtonFemale == null) {
			RadioButtonFemale = new JRadioButton();
			RadioButtonFemale.setBounds(new Rectangle(209, 150, 85, 22));
			RadioButtonFemale.setText("Female");
		}
		return RadioButtonFemale;
	}

	private JTextField getTxtAddress() {
		if (txtAddress == null) {
			txtAddress = new JTextField();
			txtAddress.setLocation(new Point(135, 226));
			txtAddress.setSize(new Dimension(166, 24));
		}
		return txtAddress;
	}

	private JDateChooser getTxtDate() {
		if (txtDate == null) {
			txtDate = new JDateChooser();
			txtDate.setDateFormatString("MM/dd/yyyy");
			txtDate.setSize(new Dimension(166, 24));
			txtDate.setLocation(new Point(134, 189));
			txtDate.getDateEditor().setEnabled(false);
		}
		return txtDate;
	}
		
	private JPanel getPanelImage() {
		if (PanelImage == null) {
			lbimage = new JLabel();
			lbimage.setText("");
			lbimage.setSize(new Dimension(106, 100));
			lbimage.setLocation(new Point(4, 2));
			PanelImage = new JPanel();
			PanelImage.setLayout(null);
			PanelImage.setBounds(new Rectangle(330, 72, 118, 119));
			PanelImage.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(51, 51, 255), 1), "Image", TitledBorder.CENTER, TitledBorder.BOTTOM, new Font("Dialog", Font.BOLD, 12), SystemColor.activeCaption));
			PanelImage.add(lbimage, null);
		}
		return PanelImage;
	}

	private JTextField getTtxImage() {
		if (ttxImage == null) {
			ttxImage = new JTextField();
			ttxImage.setLocation(new Point(135, 260));
			ttxImage.setSize(new Dimension(166, 24));
		}
		return ttxImage;
	}

	private JButton getBtnImg() {
		if (btnImg == null) {
			btnImg = new JButton();
			btnImg.setText("Browse");
			btnImg.setLocation(new Point(310, 259));
			btnImg.setSize(new Dimension(79, 26));
		}
		return btnImg;
	}

	private JButton getBtnOK() {
		if (btnOK == null) {
			btnOK = new JButton();
			btnOK.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnOK.setIcon(new ImageIcon(getClass().getResource("/image/Save.png")));
			btnOK.setText("Save");
			btnOK.setLocation(new Point(334, 340));
			btnOK.setSize(new Dimension(91, 28));
			btnOK.setHorizontalAlignment(SwingConstants.LEFT);
			btnOK.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// validate
				    	 CriminalRegModel model = new CriminalRegModel();
							if(panelCriminal.IdCriminal>=0)
							{
								model.setId(panelCriminal.IdCriminal);
							}else if( panelPrisoner.IdCriminal>=0)
							{
								model.setId(panelPrisoner.IdCriminal);
							}else if(panelMostWanted.IdCriminal>=0)
							{
								model.setId(panelMostWanted.IdCriminal);
							}
							model.setFirstName(txtFname.getText());
							model.setLastName(txtLname.getText());
							SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
							String da = sdf.format(txtDate.getDate());
							model.setBirth(da);
							model.setAddress(txtAddress.getText());
							if(RadioButtonMale.isSelected())
							{
								model.setSex(1);
							}
							else
							{
								model.setSex(0);
							}
							model.setImage(ttxImage.getText());
							model.setIdUser(mainForm.id);
							java.util.Date now = new java.util.Date();
							String d = sdf.format(now);
							model.setLastUpdated(d);
							// process add User
							if(panelCriminal.IdCriminal>=0 || panelPrisoner.IdCriminal>=0 || panelMostWanted.IdCriminal>=0)
							{
								int rs = Criminal_RegDao.updateCriminalReg(model);
								if(rs==1)
								{
									JOptionPane.showMessageDialog(null, "Update sucessful!");
									loadData.datacriminal();
									loadData.datamostwanted();
									loadData.dataprisoner();
									if(panelCriminal.IdCriminal>=0)
									{
										if(login.idrole==0)
											loadData.tab=2;
										else
											loadData.tab=1;
									}else if( panelPrisoner.IdCriminal>=0)
									{
										if(login.idrole==0)
											loadData.tab=3;
										else
											loadData.tab=2;
									}else if(panelMostWanted.IdCriminal>=0)
									{
										if(login.idrole==0)
											loadData.tab=7;
										else
											loadData.tab=6;
									}
									
									mainForm a = new mainForm();
									a.setVisible(true);
									dispose();
								}
								else
								{
									JOptionPane.showMessageDialog(null, "Update failed!");
									
								}
							}
							else
								{
									int rs = Criminal_RegDao.insertCriminalReg(model);
									if(rs==1)
									{
										JOptionPane.showMessageDialog(null, "Insert sucessful!");
										loadData.datacriminal();
										loadData.datamostwanted();
										loadData.dataprisoner();
										loadData.tab=2;
										mainForm a = new mainForm();
										a.setVisible(true);	
										dispose();
									}
									else
									{
										JOptionPane.showMessageDialog(null, "Insert failed!");
										if(login.idrole==0)
											loadData.tab=2;
										else
											loadData.tab=1;
										mainForm a = new mainForm();
										a.setVisible(true);	
										dispose();
									}
									
								}
				}	
			});
		}
		return btnOK;
	}

	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnCancel.setIcon(new ImageIcon(getClass().getResource("/image/Close.png")));
			btnCancel.setText("Exit");
			btnCancel.setLocation(new Point(202, 341));
			btnCancel.setSize(new Dimension(84, 28));
			btnCancel.setHorizontalAlignment(SwingConstants.LEFT);
			btnCancel.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					(new mainForm()).setVisible(true);
					dispose();
				}
			});
		}
		return btnCancel;
	}
	
	private void loadDataByID()
	{
		if(panelCriminal.IdCriminal>=0)
		{
		lbtitle.setText("Update User");
		lbtitle.setSize(new Dimension(140, 28));
		panelTitle.setSize(new Dimension(140, 28));
		btnOK.setIcon(new ImageIcon(getClass().getResource("/image/Save.png")));
		btnOK.setText("Save");
		btnOK.setSize(new Dimension(90, 28));
		ArrayList<CriminalRegModel> Crime = Criminal_RegDao.getbyID(panelCriminal.IdCriminal);
		for(CriminalRegModel model : Crime)
		{
			txtFname.setText(model.getFirstName());
			txtLname.setText(model.getLastName());
			try{
				SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
				txtDate.setDate(sdf.parse(model.getBirth()));
				}catch (Exception e) {
					e.printStackTrace();
				}
				txtAddress.setText(model.getAddress());
				if(model.getSex()==1)
				{
					RadioButtonMale.setSelected(true);
				}
				else
				{
					RadioButtonFemale.setSelected(true);
				}
			ttxImage.setText(model.getImage());
			txtuser.setText(userdao.getName(mainForm.id));
		
		}
	}
	}

	private JTextField getTxtuser() {
		if (txtuser == null) {
			txtuser = new JTextField();
			txtuser.setLocation(new Point(133, 300));
			txtuser.setEnabled(false);
			txtuser.setSize(new Dimension(166, 24));
		}
		return txtuser;
	}


} 
